package com.hbm.usercenter;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hbm.usercenter.activity.LogAndRegActivity;
import com.hbm.usercenter.activity.MyApplyActivity;
import com.hbm.usercenter.activity.MyOrderActivity;
import com.hbm.usercenter.activity.MyPromoteActivity;
import com.hbm.usercenter.activity.MyQrcodeActivity;
import com.hbm.usercenter.activity.MyUserInfoActivity;
import com.hbm.usercenter.base.BaseActivity;
import com.hbm.usercenter.base.LoadingPager.LoadedResult;
import com.hbm.usercenter.bean.UserCenterBean;
import com.hbm.usercenter.conf.Constants;
import com.hbm.usercenter.http.MyCallBack;
import com.hbm.usercenter.http.NetClient;
import com.hbm.usercenter.protocol.UserCenterProtocol;
import com.hbm.usercenter.utils.LogUtils;
import com.hbm.usercenter.utils.UIUtils;
import com.hbm.usercenter.widget.GroupOption;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends BaseActivity {

    @Bind(R.id.toolbar_title)
    TextView mToolbarTitle;
    @Bind(R.id.iv_apply)
    ImageView mIvApply;
    @Bind(R.id.iv_header)
    CircleImageView mIvHeader;
    @Bind(R.id.ll_name)
    LinearLayout mLlName;
    @Bind(R.id.vl)
    View mVl;
    @Bind(R.id.money)
    TextView mMoney;
    @Bind(R.id.tv_money)
    TextView mTvMoney;
    @Bind(R.id.gold)
    TextView mGold;
    @Bind(R.id.tv_gold)
    TextView mTvGold;
    @Bind(R.id.yuanbao)
    TextView mYuanbao;
    @Bind(R.id.tv_yuanbao)
    TextView mTvYuanbao;
    @Bind(R.id.fx_money)
    TextView mFxMoney;
    @Bind(R.id.tv_fx_money)
    TextView mTvFxMoney;
    @Bind(R.id.go_userinfo)
    GroupOption mGoUserinfo;
    @Bind(R.id.go_qrcode)
    GroupOption mGoQrcode;
    @Bind(R.id.go_money)
    GroupOption mGoMoney;
    @Bind(R.id.go_address)
    GroupOption mGoAddress;
    @Bind(R.id.go_team)
    GroupOption mGoTeam;
    @Bind(R.id.go_promote)
    GroupOption mGoPromote;
    @Bind(R.id.go_order)
    GroupOption mGoOrder;
    @Bind(R.id.tv_login_name)
    TextView mTvLoginName;
    @Bind(R.id.tv_group_name)
    TextView mTvGroupName;
    private UserCenterProtocol mProtocol;
    private UserCenterBean mData;

    @Override
    protected LoadedResult initData() {
        mProtocol = new UserCenterProtocol();
        try {
            mData = mProtocol.loadData(0);
            UserCenterBean.UserFinanceBean user_finance = mData.user_finance;
            UserCenterBean.UserInfoBeanC user_info = mData.user_info;
            LoadedResult result1 = checkState(user_finance);
            LoadedResult result2 = checkState(user_info);
            if (result1 != LoadedResult.SUCCESS || result2 != LoadedResult.SUCCESS) {
                return LoadedResult.EMPTY;
            } else {
                LogUtils.s(mData.toString());
                return LoadedResult.SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return LoadedResult.ERROR;
        }
    }

    @Override
    protected View initSuccessView() {
        View view = View.inflate(this, R.layout.activity_main, null);
        //        View view = LayoutInflater.from(this).inflate(R.layout.activity_my_user_info,
        // null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    protected void initView() {
        mToolbarTitle.setText("惠学科技-会员中心");
        UserCenterBean.UserFinanceBean user_finance = mData.user_finance;
        UserCenterBean.UserInfoBeanC user_info = mData.user_info;

        String loginName = user_info.getLogin_name();
        if (null != loginName) {
            mTvLoginName.setText(loginName);
        }

        mTvGroupName.setText("尊敬的" + user_info.getGroup_name() + "级会员，欢迎回来");
        String user_pic = user_info.getUser_pic();
        if (TextUtils.isEmpty(user_pic)) {
            mIvHeader.setImageDrawable(this.getResources().getDrawable((R.mipmap.tou)));
        } else {
            UIUtils.getPicasso().with(this).load("http://common.huibaoming.cn/"+user_pic).into(mIvHeader);

//            Picasso.with(this).load(user_pic).into(mIvHeader);
        }


        mTvMoney.setText(user_finance.getMoney());
        mTvGold.setText(user_finance.getGold());
        mTvYuanbao.setText(user_finance.getFx_money());
        mTvFxMoney.setText(user_finance.getFx_money());

    }

    @OnClick({R.id.iv_apply, R.id.go_userinfo, R.id.go_qrcode, R.id.go_money, R.id.go_address, R
            .id.go_team, R.id.go_promote, R.id.go_order,R.id.btn_loginout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_apply:
                startActivity(new Intent(this, MyApplyActivity.class));
                break;
            case R.id.go_userinfo:
                startActivity(new Intent(this, MyUserInfoActivity.class));
                break;
            case R.id.go_qrcode:
                startActivity(new Intent(this, MyQrcodeActivity.class));
                break;
            case R.id.go_money:
                break;
            case R.id.go_address:
                break;
            case R.id.go_team:
                break;
            case R.id.go_promote:
                startActivity(new Intent(this, MyPromoteActivity.class));
                break;
            case R.id.go_order:
                startActivity(new Intent(this, MyOrderActivity.class));
                break;
            case R.id.btn_loginout:
                logOout();
                break;
        }
    }

    private void logOout() {
        NetClient.getNetClient().callNet(Constants.URLS.LOGOUTURL, new MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String json) {
                startActivity(new Intent(UIUtils.getContext(), LogAndRegActivity.class));
                finish();
            }
        });
    }
}
